using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Adex;
using MediaMonitor.Common;
using System.Net;

namespace MediaMonitor
{
    public partial class Login : Adex.BasePage
    {

        protected void page_load(Object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                string retUrl = null;
                retUrl = Utility.GetRequestedVal("ReturnUrl", "");

                HyperLink lnkReg = this.loginx.FindControl("lnkReg") as HyperLink;
                string heading = null;
                if (!string.IsNullOrEmpty(retUrl))
                {
                    if (retUrl.Contains("/users/"))
                    {
                        heading = "User's login required";
                        if ((lnkReg != null))
                            lnkReg.NavigateUrl = Utility.AppPath + "/register.aspx?type=3&returnUrl=" + retUrl;
                    }
                    else if (retUrl.Contains("/client/"))
                    {
                        heading = "Company's login required";
                        if ((lnkReg != null))
                            lnkReg.NavigateUrl = Utility.AppPath + "/register.aspx?type=2&returnUrl=" + retUrl;
                    }
                    else if (retUrl.Contains("/admin/"))
                    {
                        heading = "Admin's login required";
                        if ((lnkReg != null))
                            lnkReg.NavigateUrl = Utility.AppPath + "/register.aspx?type=1&returnUrl=" + retUrl;
                    }
                    else
                    {
                        heading = "User's login required";
                        if ((lnkReg != null))
                            lnkReg.NavigateUrl = Utility.AppPath + "/register.aspx?type=3&returnUrl=" + retUrl;
                    }
                }
                else
                {
                    heading = "User's login required";
                    if ((lnkReg != null))
                        lnkReg.NavigateUrl = Utility.AppPath + "/register.aspx?type=3&returnUrl=" + retUrl;
                }

                this.ltrHeading.Text = heading;

            }

        }

        protected void login_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
        {
            Int32 UserID = 0;

            if (System.Web.Security.Membership.ValidateUser(loginx.UserName, loginx.Password))
            { 
                SiteUser usr = AppUser.GetUserByUserName(loginx.UserName);
                 
                AppUser.SignIn(usr.SiteUserID, loginx.RememberMeSet);
                UserID = usr.SiteUserID;
                Response.Cookies["User"]["UserID"] = UserID.ToString();
                Response.Cookies["User"]["Company"] = usr.Company.ToString();
                Response.Cookies["User"]["Name"] = usr.FullName;
                Response.Cookies["User"]["CompanyCode"] = usr.CompanyCode.ToString();
               // Response.Cookies["User"]["UserRole"] = dt.Rows[0][2].ToString();
                Response.Cookies["User"]["UserName"] = usr.UserName;
                Response.Cookies["User"].Expires = DateTime.Now.AddYears(1);
                if ((Request["ReturnUrl"] != null))
                {
                    String Val=Server.UrlDecode(Request["ReturnUrl"]).ToString();

                    if (Val.Contains("users") || Val.Contains("Users"))
                    {
                        if (UserID > 0)
                        {
                            SaveLoginUserDetails(UserID);
                        }
                       
                    }

                    Response.Redirect(Server.UrlDecode(Request["ReturnUrl"]));
                }
                else
                {
                    if (UserID > 0)
                    {
                        SaveLoginUserDetails(UserID);
                    }
                    Response.Redirect("~/users/default.aspx");
                }



            }
            else
            {
                AjaxFuncs.LoginScreen(this.Page, true);
            }
        }

        private void SaveLoginUserDetails(Int32 UserID)
        {
            String stringIpAddress = String.Empty;

            //Get IP Address
            stringIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (stringIpAddress == null) //may be the HTTP_X_FORWARDED_FOR is null
            {
                stringIpAddress = Request.ServerVariables["REMOTE_ADDR"];//we can use REMOTE_ADDR
            }

            if (!String.IsNullOrEmpty(stringIpAddress))
            {
                RfController.SiteUser_Insert(UserID, stringIpAddress);
            }
        }

    }//end class
}
